package com.jumi.microservice.material.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.jumi.microservice.common.exception.ExceptionSupply;
import com.jumi.microservice.framework.log.domain.SysOperLog;
import com.jumi.microservice.material.mapper.SysOperLogMapper;
import com.jumi.microservice.material.service.ISysOperLogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 操作日志 服务层处理
 *
 * @author ruoyi
 */
@Service
public class SysOperLogServiceImpl implements ISysOperLogService
{
    @Resource
    private SysOperLogMapper operLogMapper;

    /**
     * 新增操作日志
     *
     * @param operLog 操作日志对象
     * @return 结果
     */
    @Override
    public int insertOperlog(SysOperLog operLog)
    {
        return operLogMapper.insertOperlog(operLog);
    }

    /**
     * 查询系统操作日志集合
     *
     * @param operLog 操作日志对象
     * @return 操作日志集合
     */
    @Override
    public List<SysOperLog> selectOperLogList(SysOperLog operLog)
    {
        if(StringUtils.isNotBlank(operLog.getAction())){

            try {
                String str=this.operNameCb(0,operLog.getAction()).toString();
                Integer businessType=Integer.parseInt(str);
                operLog.setBusinessType(businessType);
            } catch (Exception e) {
                throw new ExceptionSupply(500,"操作类型不存在!").newException();
            }

        }
        operLog.setStatus(0);
        operLog.setJsonResult("\"code\":200");
        List<SysOperLog> list=operLogMapper.selectOperLogList(operLog);
        for(SysOperLog obj:list){
            String action=this.operNameCb(obj.getBusinessType(),null).toString();
            obj.setAction(action);
        }
        return list;
    }

    private Object operNameCb(int index,String action){
        String[] actions={"其他操作","新增操作","修改操作","删除操作"
        ,"授权操作","导出操作","导入操作","强退","生成代码","清空数据","审核"};
        if(StringUtils.isNotBlank(action)){
            for(int i=0;i<actions.length;i++){
                if (actions[i].contains(action)){
                    return i;
                }
            }
        }else{
            return actions[index];
        }
        return null;
    }
    /**
     * 批量删除系统操作日志
     *
     * @param operIds 需要删除的操作日志ID
     * @return 结果
     */
    public int deleteOperLogByIds(Long[] operIds)
    {
        return operLogMapper.deleteOperLogByIds(operIds);
    }

    /**
     * 查询操作日志详细
     *
     * @param operId 操作ID
     * @return 操作日志对象
     */
    @Override
    public SysOperLog selectOperLogById(Long operId)
    {
        return operLogMapper.selectOperLogById(operId);
    }

    /**
     * 清空操作日志
     */
    @Override
    public void cleanOperLog()
    {
        operLogMapper.cleanOperLog();
    }
}
